package tech.brick.easysharer.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class HeartbeatScheduler {

    private final ServiceRegistryService serviceRegistryService;
    @Value("${service.discover.enabled:true}")
    private Boolean serviceDiscoverEnabled;
    /**
     * 每10分钟发送一次心跳
     * 固定延迟10分钟执行
     */
    @Scheduled(fixedDelay = 600000) // 10分钟 = 600000毫秒
    public void sendHeartbeat() {
        if (!serviceDiscoverEnabled) {
            return;
        }
        log.debug("开始发送心跳...");
        boolean success = serviceRegistryService.sendHeartbeat();
        if (success) {
            log.debug("心跳发送成功");
        } else {
            log.warn("心跳发送失败, 尝试重新注册");
            serviceRegistryService.registerService();
        }
    }
}
